package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.service.ImageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@Service(value = "imageService")
public class ImageServiceImpl implements ImageService {


    @Value("${imageFilePath}")
    private String imageFilesPath;

    private static String URL_PATH = "https://dormassistant.wang/images/" ;

    /**
     *
     * @param file 待上传的文件
     * @return 是否成功
     */
    @Override
    public String addImage(MultipartFile file) {

        if(file != null) {
            String fileName = UUID.randomUUID().toString().replaceAll("-", "") + file.getOriginalFilename();
            String destFileName = imageFilesPath + File.separator + fileName;
            File destFile = new File(destFileName);
            try {
                file.transferTo(destFile);
            } catch (IOException e) {
                e.printStackTrace();
                return "https://dormassistant.wang/images/46601b06939a4933ab5e6c030322d5d1wx0a109bd236f8156f.o6zAJs4ke-kdwrH6tAO58xhiBcnI.3jirDM0nukjOd94caecbd6bd7e4f89f2e50e872eaa29.jpeg";
            }
            return URL_PATH + fileName;

        }else{
            return "https://dormassistant.wang/images/46601b06939a4933ab5e6c030322d5d1wx0a109bd236f8156f.o6zAJs4ke-kdwrH6tAO58xhiBcnI.3jirDM0nukjOd94caecbd6bd7e4f89f2e50e872eaa29.jpeg";
        }

    }
}
